import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import axios from "axios";

const API_URL = 'http://geek.itheima.net/v1_0/channels'


// defineStore('counter') 定义了一个模块 counter
export const useCounterStore = defineStore('counter', () => {
  // 定义数据(state)
  const count = ref(0)

  // 计算属性
  const doubleCount = computed(() => count.value * 2)

  // 定义修改数据的方法(同步action)
  const increment = () => {
    count.value++
  }

  // 定义异步action的方法
  const list = ref([])
  const getList = async () => {
    const res = await axios.get(API_URL)
    list.value = res.data.data.channels
  }

  return {
    count,
    doubleCount,
    increment,
    list,
    getList
  }
},{
  // 持久化配置
  persist: true
})
